<?php

namespace App\Http\Controllers\home;

// use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Session\Session;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Requests;
use Request;

class OrderController extends BaseController
{
    //订单列表
    public function index()
    {
        $user_name = Cookie::get('u_name');
        // echo $user_name;die;
        $user = DB::table('user') -> select("user_id") -> where("user_name",$user_name) -> first();
        $user_id = $user->user_id;

        $order = DB::table("order as o")
                 -> leftJoin("user as u","u.user_id","o.user_id")
                 -> orderBy("order_addtime","DESC")
                 -> first();
        $paymethod = array(
            '0' => "未确认订单",
            '1' => "已付款",
            '2' => "未付款",
        );

        $addtime = $order -> order_addtime;
        $addtime = date("Y-m-d",$addtime);
        $order -> order_addtime = $addtime;

        $status = $order -> is_pay;
        $order -> is_pay = $paymethod[$status];


        return view('home/order',compact("order"));
    }

    //添加订单
    public function create()
    {
        $input = Request::all();

        $user_name = Cookie::get('u_name');
        // echo $user_name;die;
        $user = DB::table('user') -> whereRaw("user_name = '$user_name'") -> first();
        $user_id = $user->user_id;

        $address ='';//存储地址
        //省份/直辖市
        $county_id = $input['county'];//省份/直辖市id
        $county = DB::table("city") -> where("city_id",$county_id) -> first();
        $county = $county -> city_name;
     
        //城市
        $city_id = $input['city'];//城市id
        $city = DB::table("city") -> where("city_id",$city_id) -> first();
        $city = $city -> city_name;
        //乡镇
        $town_id = $input['town'];//乡镇id
        $town = DB::table("city") -> where("city_id",$town_id) -> first();
        $town = $town -> city_name;
        $area = $input['area'];
        $address .=$county.' '.$city.' '.$town.' '.$area;

        //订单号
        // $order_number ='';//存储订单号
        // $arr = "ABCDEFHIJKLMNOPQRSTUVWXYZ";
        $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
        $order_number = $yCode[intval(date('Y')) - 2020] . strtoupper(dechex(date('m'))) . date('d') .
            substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
        
        $data = array(
            'user_id' => $user_id,
            'consignee' => $input['consignee'],
            'telephone' => $input['telphone'], 
            'address' => $address, 
            'is_pay' => $input['paymethod'],
            'pay_price' => $input['total'],
            'order_number' => $order_number,
            'order_addtime' => time(),
            'is_sign' => '1',
            'is_consign' => '1',
        );
        $res = DB::table("order") -> insert($data);
        if($res){
            show_msg("下单成功","/order");
            // return redirect("/order");
        }else{
            return redirect("/checkout");
        }
    }
    
}
